﻿using WCS.Repository.Interface;
using WCS.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Storage;

namespace WCS.Service.EFCore
{
    public class BaseEFService<T> : IBaseEFService<T> where T : class, new()
    {
        private readonly IRepository<T> _repository;

        public BaseEFService(IRepository<T> repository)
        {
            _repository = repository;
        }

        public Task AddAsync(T t, CancellationToken cancellationToken = default)
        {
            return _repository.AddAsync(t, cancellationToken);
        }

        public Task AddRangeAsync(IEnumerable<T> ie, CancellationToken cancellationToken = default)
        {
            return _repository.AddRangeAsync(ie, cancellationToken);
        }

        public Task DeleteAsync(T t, CancellationToken cancellationToken = default)
        {
            return _repository.DeleteAsync(t, cancellationToken);
        }

        public Task DeleteRangeAsync(IEnumerable<T> ie, CancellationToken cancellationToken = default)
        {
            return _repository.DeleteRangeAsync(ie, cancellationToken);
        }

        public Task<T?> FindAsync(params object?[]? values)
        {
            return _repository.FindAsync(values);
        }

        public Task<T?> FindAsync(object?[]? values, CancellationToken cancellationToken = default)
        {
            return _repository.FindAsync(values, cancellationToken);
        }

        public Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        {
            return _repository.SaveChangesAsync(cancellationToken);
        }

        public Task UpdateAsync(T t, CancellationToken cancellationToken = default)
        {
            return _repository.UpdateAsync(t, cancellationToken);
        }

        public  Task UpdateRangeAsync(IEnumerable<T> ie, CancellationToken cancellationToken = default)
        {
            return _repository.UpdateRangeAsync(ie, cancellationToken);
        }

        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where)
        {
            return _repository.QueryAll(where);
        }

        public IQueryable<T> QueryAll<type>(Expression<Func<T, bool>>? where, Expression<Func<T, type>>? order, bool isAsc = true)
        {
            return _repository.QueryAll(where, order, isAsc);
        }
        public IQueryable<T> QueryAll<type>(out int total, int page, int limit, Expression<Func<T, bool>>? where, Expression<Func<T, type>>? order, bool isAsc = true)
        {
            return _repository.QueryAll(out total, (page - 1) * limit, limit, where, order, isAsc);
        }
    }
}
